package com.pfzy.ui.activity.notification;

import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.pfzy.ui.R;

public class NotificationDialog5 {
    private AlertDialog mDialog;
    private Context mContext;
    private TextView mTvTime;
    private OnNotificationClick mListener;
    private static NotificationDialog5 instance;
    private int mTime = 3;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mTime--;
            mTvTime.setText(mTime + "s");
            if (mTime == 0) {
                this.removeMessages(0);
                mDialog.dismiss();
            }
            sendEmptyMessageDelayed(0, 1000);
        }
    };

    public static NotificationDialog5 getInstance(Context context) {
        if (instance == null) {
            synchronized (NotificationDialog5.class) {
                if (instance == null) {
                    instance = new NotificationDialog5(context);
                }
            }
        }
        return instance;
    }

    NotificationDialog5(Context context) {
        _logd("constructor");
        mContext = context;

        AlertDialog.Builder builder = new AlertDialog.Builder(context, R.style.dialog_notifacation_top);
        mDialog = builder.create();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
        } else {
            mDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }
        mDialog.getWindow().setGravity(Gravity.TOP);

        WindowManager.LayoutParams layoutParams = mDialog.getWindow().getAttributes();
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        mDialog.getWindow().setAttributes(layoutParams);
        mDialog.getWindow().setWindowAnimations(R.style.dialog_animation);
        mDialog.setCanceledOnTouchOutside(true);
    }

    public void showNotificationDialog(String title, String content, int image, OnNotificationClick listener) {
        _logd("showNotificationDialog");
        mDialog.show();
        View mView = View.inflate(mContext, R.layout.layout_notifacation3, null);
        ConstraintLayout mLayout = mView.findViewById(R.id.layout_notifacation3_layout);
        TextView mTvTitle = mView.findViewById(R.id.layout_notifacation3_title);
        TextView mTvContent = mView.findViewById(R.id.layout_notifacation3_content);
        ImageView mImage = mView.findViewById(R.id.layout_notifacation3_image);
        mTvTime = mView.findViewById(R.id.layout_notifacation3_time);
        mDialog.getWindow().setContentView(mView);

        if (listener != null) {
            setOnNotificationClickListener(listener);
        }

        mLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onClick();
            }
        });

        mTvTitle.setText(title);

        if (content != null && content.length() > 0) {
            mTvContent.setText(content);
        }

        if (image != 0) {
            mImage.setImageResource(image);
        }

        mTime = 3;
        if(!handler.hasMessages(0)){
            _logd("发送定时");
            handler.sendEmptyMessageDelayed(0,1000);
        }
    }

    private void setOnNotificationClickListener(OnNotificationClick listener) {
        mListener = listener;
    }

    public interface OnNotificationClick {
        void onClick();
    }

    private void _logd(String msg) {
        Log.d("全局弹窗", msg);
    }
}
